package com.ruicar.afs.cloud.bizcommon.rules.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.bizcommon.enums.rules.RuleOperateType;
import com.ruicar.afs.cloud.bizcommon.enums.rules.RuleStatus;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.mapper.AfsRuleInfoMapper;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleOperateHisService;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRulePublishService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.MessageFormat;
import java.util.List;


// 规则组件内部用，禁止其他组件调用
// 规则组件内部用，禁止其他组件调用
// 规则组件内部用，禁止其他组件调用
// 规则组件内部用，禁止其他组件调用
// 规则组件内部用，禁止其他组件调用

@Service
@Slf4j
@AllArgsConstructor
public class AfsRulePublishServiceImpl implements AfsRulePublishService {
    private final AfsRuleInfoMapper afsRuleInfoMapper;
    private final AfsRuleOperateHisService afsRuleOperateHisService;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void active(Long id) {
        AfsRuleInfo afsRuleInfo = afsRuleInfoMapper.selectById(id);
        Assert.isTrue(afsRuleInfo != null, "输入规则编号不存在");
        if (RuleStatus.ACTIVE == afsRuleInfo.getStatus()) {
            return;
        }
        List<AfsRuleInfo> otherRules = afsRuleInfoMapper.selectList(Wrappers.<AfsRuleInfo>lambdaQuery().ne(AfsRuleInfo::getId,id).eq(AfsRuleInfo::getStatus,RuleStatus.ACTIVE).eq(AfsRuleInfo::getRuleNo,afsRuleInfo.getRuleNo()));
        Assert.isTrue(CollectionUtil.isEmpty(otherRules), MessageFormat.format("规则编号为[{0}]，版本为[{1}]的规则已经处于激活状态,不允许激活[{2}]版本",afsRuleInfo.getRuleNo(),otherRules.isEmpty()?"":otherRules.get(0).getRuleVersion(),afsRuleInfo.getRuleVersion()));
        String endDate = afsRuleInfo.getActiveEndDate();
        Assert.isTrue(endDate != null && DateUtil.parse(endDate, DatePattern.PURE_DATETIME_PATTERN).isAfter(DateUtil.date()), "规则有效期早于当前日期不能激活规则");
        afsRuleInfo.setStatus(RuleStatus.ACTIVE);
        afsRuleInfoMapper.updateById(afsRuleInfo);
        afsRuleOperateHisService.activeRule(afsRuleInfo.getId());
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void deActive(Long id) {
        AfsRuleInfo afsRuleInfo = afsRuleInfoMapper.selectById(id);
        Assert.isTrue(afsRuleInfo != null, "输入规则编号不存在");
        if (RuleStatus.ACTIVE != afsRuleInfo.getStatus()) {
            return;
        }
        afsRuleInfo.setStatus(RuleStatus.INVALIDITY);
        afsRuleOperateHisService.modifyRule(id, id, RuleOperateType.DE_ACTIVE);
        afsRuleInfoMapper.updateById(afsRuleInfo);
    }
}
